using System.Collections.Generic;
using mvc.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using mvc.Db;

namespace mvc.Responitory
{
    public class Responitory<T> : IResponitory<T> where T : Base
    {
        private readonly DbContext _Db;
        private readonly DbSet<T> _table;

        public IQueryable<T> GetTable
        {

            get
            {
                return _table as IQueryable<T>;
            }
        }
        public Responitory()
        {
            _Db = new AspNetCoreMvcDb();
            _table = _Db.Set<T>();
        }
        public T CheckData(int ID)
        {
            if (ID > 0)
            {
                var Entitys = (from Tb in _table where Tb.IsDeleted == false && Tb.Id == ID select Tb).FirstOrDefault();
                return Entitys;

            }
            else
            {
                throw new System.NotImplementedException();
            }
        }

        public void DeleteData(int ID)
        {
            if (ID > 0)
            {
                var Entity = (from Tb in _table where Tb.IsDeleted == false && Tb.Id == ID select Tb).FirstOrDefault();
                Entity.IsDeleted=true;
                UpateData(Entity);
            }
            else
            {
                throw new System.NotImplementedException();
            }
        }

        public void InsertData(T Entity)
        {
            if (Entity != null)
            {
                _table.Add(Entity);
                _Db.SaveChanges();
            }
             else
            {

                throw new System.NotImplementedException();
            }
        }

        public void UpateData(T Entity)
        {
            if (Entity != null)
            {
                _table.Update(Entity);
                _Db.SaveChanges();
            }
            else
            {

                throw new System.NotImplementedException();
            }
        }

     
    }
}